package com.hzit.sk.controller;

import com.hzit.sk.model.SkillGoodsInfo;
import com.hzit.sk.resp.GoodsDeatailData;
import com.hzit.sk.resp.Response;
import com.hzit.sk.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品相关控制器
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private IGoodsService GoodsService;

    @RequestMapping("/list")
    public String getSkillGoods(Model model){
        logger.info("获取秒杀商品列表：");
//        Response response = new Response();
        List<SkillGoodsInfo>  goodsList = GoodsService.querySkillGoods();
        model.addAttribute("goodsList",goodsList);
//        response = response.getSuccessData("查询成功");
//        response.setData(list);
        return  "goods_list";

    }

    /**
     * 秒杀商品详细信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/detail")
    public Response<GoodsDeatailData> getGoodsDeatail(@RequestParam("goodsId") String goodsId,
                                                      @RequestParam("userId") String userId){
        logger.info("接收到用户id：{}获取秒杀商品：{}详细信息的请求.....",userId,goodsId);
//        Response<GoodsDeatailData> response = new Response<GoodsDeatailData>();

        if (StringUtils.isEmpty(goodsId)){
            return Response.getFailData("商品id不能为空");
        }

        if (StringUtils.isEmpty(userId)){
            return Response.getFailData("userId不能为空");
        }

        GoodsDeatailData goodsDeatailData =  GoodsService.queryGoodsDeatailData(userId,goodsId);
        Response<GoodsDeatailData> response =  Response.getSuccessData("请求成功");
        response.setData(goodsDeatailData);
        return response;

    }


}
